
package Monster

import(
	"fmt"
	"sanguo/node/table/excel"
	"sanguo/node/table/excel/table"
	"strconv"

	"github.com/tealeg/xlsx"
)

func readInt32(v string) int32 {
	return int32(readFloat(v))
}

func readFloat(v string) float64 {
	vv,err := strconv.ParseFloat(v,64)
	if nil != err {
		panic(err)
	}
	return vv
}

func readBool(str string) bool {
    b, err := strconv.ParseBool(str)
	if nil != err {
		panic(err)
	}
    return b
}

func readStr(v string) string {
	return fmt.Sprintf("%s",v)
}

type Monster struct {
	ID int32
	Level int32
	Type int32
	Exp int32
	Gold int32
	DropPool int32
	Attack int32
	Defenses int32
	HitPoint int32
	ArmorPoiseHealth int32
	MovementSpeed int32
	CritChance int32
	CritPower int32
	CritDamage int32
	CritResistance int32
	PhysicalResistance int32
}

type Table struct {
	IDMap map[int32]*Monster
	tmpIDMap map[int32]*Monster
	xlsxName string
}

var Table_ Table

func getIdxByName(names *xlsx.Row, name string) int {
	for i, v := range names.Cells {
		if v.String() == name {
			return i
		}
	}
	return -1
}

func init() {
	Table_.xlsxName = "Monster.xlsx"
	Table_.IDMap = make(map[int32]*Monster)
	Table_.tmpIDMap = make(map[int32]*Monster)

	table.AddTable(&Table_)
}


func (this *Table) Load(path string) {

	excel.EmptyFunc()

    xlFile, err := xlsx.OpenFile(path + this.xlsxName)
    if err != nil {
		panic(err)
    }

    sheet := xlFile.Sheets[0]
    names := sheet.Rows[1]
    rows  := sheet.Rows[3:]

    for _,row := range(rows) {
    	if len(row.Cells) > 0 && row.Cells[0].String() != "" {
    		ele := &Monster{}
    	
		ele.ID = readInt32(row.Cells[getIdxByName(names, "ID")].String())
		this.tmpIDMap[ele.ID] = ele
		ele.Level = readInt32(row.Cells[getIdxByName(names, "Level")].String())
		ele.Type = readInt32(row.Cells[getIdxByName(names, "Type")].String())
		ele.Exp = readInt32(row.Cells[getIdxByName(names, "Exp")].String())
		ele.Gold = readInt32(row.Cells[getIdxByName(names, "Gold")].String())
		ele.DropPool = readInt32(row.Cells[getIdxByName(names, "DropPool")].String())
		ele.Attack = readInt32(row.Cells[getIdxByName(names, "Attack")].String())
		ele.Defenses = readInt32(row.Cells[getIdxByName(names, "Defenses")].String())
		ele.HitPoint = readInt32(row.Cells[getIdxByName(names, "HitPoint")].String())
		ele.ArmorPoiseHealth = readInt32(row.Cells[getIdxByName(names, "ArmorPoiseHealth")].String())
		ele.MovementSpeed = readInt32(row.Cells[getIdxByName(names, "MovementSpeed")].String())
		ele.CritChance = readInt32(row.Cells[getIdxByName(names, "CritChance")].String())
		ele.CritPower = readInt32(row.Cells[getIdxByName(names, "CritPower")].String())
		ele.CritDamage = readInt32(row.Cells[getIdxByName(names, "CritDamage")].String())
		ele.CritResistance = readInt32(row.Cells[getIdxByName(names, "CritResistance")].String())
		ele.PhysicalResistance = readInt32(row.Cells[getIdxByName(names, "PhysicalResistance")].String())

		}
    }

    this.IDMap = this.tmpIDMap
 
}

